package main

/*
grpc 内置状态码以及错误信息error
*/
import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"net"
	"time"

	"google.golang.org/grpc"
	pb "grpc_demo/grpc_error/grpc_error"
)

type Server struct {
	pb.UnimplementedGreeterServer
}

func (s *Server) SayHello(ctx context.Context, request *pb.HelloRequest) (*pb.HelloReply,
	error) {
	// grpc内置状态码及错误模块
	time.Sleep(10 * time.Second)
	return nil, status.Errorf(codes.NotFound, "记录未找到：%s", request.Name)
	//return nil, errors.New("error")

}

func main() {
	g := grpc.NewServer()
	pb.RegisterGreeterServer(g, &Server{})
	lis, err := net.Listen("tcp", "0.0.0.0:50051")
	if err != nil {
		panic("failed to listen:" + err.Error())
	}
	err = g.Serve(lis)
	if err != nil {
		panic("failed to start grpc:" + err.Error())
	}
}
